﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using NDK.Motion.ViewModel;
using System.Diagnostics;

namespace NDK.Motion.Controls
{
    public partial class ucDebug_CT : UserControl, INotifyPropertyChanged
    {
        private List<StepViewModel> _steps = new List<StepViewModel>();
        public event PropertyChangedEventHandler PropertyChanged;
        private string curStep = "";//当前步
        private string lastStep = "";//上一步

        private bool isMonitor = true;//是否监控CT

        [Bindable(false), Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<StepViewModel> Steps
        {
            get
            {
                return _steps;
            }

            set
            {
                _steps = value;
                Init();
            }
        }
        public ucDebug_CT()
        {
            InitializeComponent();
        }
        private void Init()
        {
            //初始化表格的行数据
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(Init));
            }
            else
            {
                foreach (var step in _steps)
                {
                    int index = dgv_CTDebug.Rows.Add();
                    //dgv_CTDebug.Rows[index].Cells["cStepStatus"].Value = Resources.LightOff;
                    dgv_CTDebug.Rows[index].Cells["cStepName"].Value = step.StepName;
                    dgv_CTDebug.Rows[index].Cells["cStepDescription"].Value = step.StepDescription;
                    dgv_CTDebug.Rows[index].Cells["cStepCT"].Value = 0.0;
                }
            }
            //dgv_CTDebug.DataSource = new BindingList<StepViewModel>(_steps);
        }
        private void ucDebug_CT_Load(object sender, EventArgs e)
        {
            PropertyChanged += UcDebug_CT_PropertyChanged;
        }

        private void ckb_Minitor_CheckedChanged(object sender, EventArgs e)
        {
            if (ckb_Minitor.Checked == true)
            {
                IsMonitor = true;
            }
            else
            {
                IsMonitor = false;
            }
        }
        /// <summary>
        /// 当前步骤属性设置，触发步骤变化事件
        /// </summary>
        public string CruStep
        {
            get
            {
                return curStep;
            }

            set
            {
                if (curStep != value)
                {
                    if (PropertyChanged != null && value != null)
                    {
                        lastStep = curStep;
                        curStep = value;
                        PropertyChanged(this, new PropertyChangedEventArgs(value.ToString()));
                    }
                }
            }
        }
        /// <summary>
        /// 控件标题
        /// </summary>
        [Browsable(true), Description("设置工位名称"), Category("自定义")]
        public string Title
        {
            get
            {
                return lbl_Name.Text;
            }
            set
            {
                lbl_Name.Text = value;
            }
        }
        [Browsable(true)]
        [Description("是否监控流程")]
        public bool IsMonitor
        {
            get
            {
                return isMonitor;
            }

            set
            {
                isMonitor = value;
            }
        }

        Stopwatch sw_CT = new Stopwatch();
        private void UcDebug_CT_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            UpdateStep();
        }
        /// <summary>
        /// 更新界面步骤显示
        /// </summary>
        //[MethodImpl(MethodImplOptions.Synchronized)]
        private void UpdateStep()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(UpdateStep));
            }
            else
            {
                //不监控直接返回
                if (isMonitor == false)
                    return;
                if (this.IsDisposed || !this.IsHandleCreated)
                {
                    return;
                }

                int count = dgv_CTDebug.RowCount;
                //更新上一步的CT和状态
                for (int i = 0; i < count; i++)
                {
                    if (lastStep == dgv_CTDebug.Rows[i].Cells["cStepName"].Value.ToString())
                    {
                        dgv_CTDebug.Rows[i].Cells["cStepCT"].Value = sw_CT.ElapsedMilliseconds.ToString();
                        //dgv_CTDebug.Rows[i].Cells["cStepStatus"].Value = Resources.LightOff;
                        dgv_CTDebug.Rows[i].Selected = false;
                        break;
                    }
                }
                //更新当前步的状态
                for (int i = 0; i < count; i++)
                {
                    if (curStep == dgv_CTDebug.Rows[i].Cells["cStepName"].Value.ToString())
                    {
                        //dgv_CTDebug.Rows[i].Cells["cStepStatus"].Value = Resources.LightOn;
                        dgv_CTDebug.Rows[i].Selected = true;
                        sw_CT.Restart();
                        break;
                    }
                }
            }
        }
    }
}
